package org.moca.db;

import java.util.Calendar;
import java.util.List;

import org.moca.procedure.Procedure;
import org.moca.procedure.ProcedureElement;
import org.moca.procedure.ValidationError;

import android.util.Log;

public class PatientValidator {
	
	private static final String TAG = PatientValidator.class.toString();

	public static boolean validate(Procedure p, PatientInfo pi) throws ValidationError {
		boolean result = true;
		
		//patient authentication checks for existing patients
		if (p.current().hasSpecialElement()) {
			if (!validateSpecialElements(p, pi)) {
				return false;
			} 
		}

		return result;
	}

	public static void populateSpecialElements(Procedure p, PatientInfo pi) {
		if (pi == null || !pi.isConfirmed() || !p.current().hasSpecialElement()) {
			return;
		}
		
		List<ProcedureElement> specialElements = p.current().getSpecialElements();
		
		for (ProcedureElement element : specialElements) {
			String id = element.getId();
			if (pi != null) {
				element.setAnswer(pi.getAnswerForId(id));
			}
		}
	}
	
	private static boolean validateSpecialElements(Procedure p, PatientInfo pi) throws ValidationError {
		List<ProcedureElement> specialElements = p.current().getSpecialElements();

		if (pi == null)
			pi = new PatientInfo();
		
		for (ProcedureElement el : specialElements) {
			String element_id = el.getId();
			String element_answer = el.getAnswer().trim().toLowerCase();
	
			if (element_id.equals("patientId")) {
				
			} else if (element_id.equals("patientFirstName")) {
	
			} else if (element_id.equals("patientLastName")) {
				
			} else if (element_id.equals("patientGender")) {
				
			} else if (element_id.equals("patientBirthdateMonth")) {
				String year = p.current().getElementValue("patientBirthdateYear");
				try {
					int yearValue = Integer.parseInt(year);
					int currentYear = Calendar.getInstance().get(Calendar.YEAR);
					Log.i(TAG, "Validating year: " + yearValue + " against " + currentYear);
					if (yearValue > currentYear || yearValue < currentYear-120) {
						throw new ValidationError("The year entered is not valid (in the future or too far in the past). Please try again.");
					}
				} catch (Exception e) {
					throw new ValidationError("The year entered is not a number. Please enter a number.");
				}
			}
		}
		return true;
	}

}
